package com.archermind.boc.login;

import org.apache.commons.codec.binary.Base64;

import com.archermind.boc.dal.business.file.IZhiWenHelper;
import com.archermind.boc.dal.business.file.impl.ZhiWenHelper;
import com.archermind.boc.domain.User;

public class LoginUtil {
	public static String decodedString(String codingcode) {
		Base64 base64 = new Base64();
		byte[] debytes = null;

		String code = new String(codingcode);
		debytes = base64.decode(code.getBytes());
		String decodedcode = new String(debytes);

		return decodedcode;
	}

	public static String[] ParseUser(String str) {
		String[] strArray = str.split(":");
		if (strArray != null && strArray.length == 2) {
			return strArray;
		}
		return null;
	}

	public static String dealString(String str) {
		return '"' + str + '"';
	}

	public static LoginResponse loginedMessage(User user) {
		LoginResponse response = new LoginResponse();
		StringBuffer str = new StringBuffer();
		if (user == null) {
			str.append("<error>登录名不存在或登录密码错误!</error>");
		} else {
			str.append("<success role=" + dealString(user.getRole())
					+ " counter=" + dealString(user.getLoginName()));
			str.append(" netnumber=" + dealString(user.getBankNet().getNetId())
					+ " name=" + dealString(user.getRealName()) + "/>");
		}

		response.setMessage(str.toString());
		return response;
	}

	public static LoginResponse sendMessage(User user) {
		IZhiWenHelper helper = ZhiWenHelper.getInstance();
		LoginResponse response = new LoginResponse();
		StringBuffer str = new StringBuffer();
		if (user == null) {
			str.append("<error>登录名不存在或指纹密码错误!</error>");
		} else {
			String path = user.getPassword();
			byte[] b = helper.read(path);
			str.append("<success role=" + dealString(user.getRole())
					+ " counter=" + dealString(user.getLoginName()));
			str.append(" netnumber=" + dealString(user.getBankNet().getNetId())
					+ " name=" + dealString(user.getRealName()));
			str.append(">" + new String(b)+ "</success>");
		}
		response.setMessage(str.toString());
		return response;
	}
}
